package com.yf.exam.modules.trainingarchives.teacherattendance.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.deepoove.poi.data.Pictures;
import com.yf.exam.ability.upload.service.impl.UploadServiceImpl;
import com.yf.exam.common.enums.DeleteCodeEnum;
import com.yf.exam.core.api.ApiError;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.core.exception.ServiceException;
import com.yf.exam.modules.trainingarchives.pdfexport.dto.second.TeacherAttendanceExportDTO;
import com.yf.exam.modules.trainingarchives.teacherattendance.dto.TeacherAttendanceDTO;
import com.yf.exam.modules.trainingarchives.teacherattendance.entity.TeacherAttendance;
import com.yf.exam.modules.trainingarchives.teacherattendance.mapper.TeacherAttendanceMapper;
import com.yf.exam.modules.trainingarchives.teacherattendance.service.ITeacherAttendanceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.exam.modules.trainingarchives.teacherattendance.vo.TeacherAttendanceListVO;
import com.yf.exam.modules.trainingarchives.teacherplanexecution.vo.TeachingPlanExecutionListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 授课教师签到表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-29
 */
@Service
public class TeacherAttendanceServiceImpl extends ServiceImpl<TeacherAttendanceMapper, TeacherAttendance> implements ITeacherAttendanceService {

    @Autowired
    private UploadServiceImpl uploadService;

    @Override
    public void add(TeacherAttendanceDTO reqDTO) {
        beasVerify(reqDTO);
        TeacherAttendance teacherAttendance = new TeacherAttendance();
        BeanUtils.copyProperties(reqDTO,teacherAttendance);
        this.save(teacherAttendance);
    }

    @Override
    public void update(TeacherAttendanceDTO reqDTO) {
        beasVerify(reqDTO);
        TeacherAttendance teacherAttendance = new TeacherAttendance();
        BeanUtils.copyProperties(reqDTO,teacherAttendance);
        this.updateById(teacherAttendance);
    }

    @Override
    public IPage<TeacherAttendanceListVO> paging(PagingReqDTO<TeacherAttendanceDTO> reqDTO) {
        return baseMapper.paging(reqDTO.toPage(),reqDTO.getParams());
    }

    @Override
    public List<TeacherAttendanceExportDTO> getTeacherAttendancePdfData(String trainingClassInfoId) {
        List<TeacherAttendanceExportDTO> teacherAttendancePdfData = baseMapper.getTeacherAttendancePdfData(trainingClassInfoId);
        for (TeacherAttendanceExportDTO teacherAttendancePdfDatum : teacherAttendancePdfData) {
            Object teacherSignOneTwo = teacherAttendancePdfDatum.getTeacherSignOneTwo();
            if (Objects.nonNull(teacherSignOneTwo) && teacherSignOneTwo.toString().contains("file")) {
                teacherAttendancePdfDatum.setTeacherSignOneTwo(Pictures.ofLocal(uploadService.getRealPath(teacherSignOneTwo.toString())).size(60, 30).center().create());
                teacherAttendancePdfDatum.setIsPictureOneTwo(true);
                teacherAttendancePdfDatum.setIsTextOneTwo(false);
            } else {
                teacherAttendancePdfDatum.setIsPictureOneTwo(false);
                teacherAttendancePdfDatum.setIsTextOneTwo(true);
            }

            Object teacherSignThreeFour = teacherAttendancePdfDatum.getTeacherSignThreeFour();
            if (Objects.nonNull(teacherSignThreeFour) && teacherSignThreeFour.toString().contains("file")) {
                teacherAttendancePdfDatum.setTeacherSignThreeFour(Pictures.ofLocal(uploadService.getRealPath(teacherSignThreeFour.toString())).size(60, 30).center().create());
                teacherAttendancePdfDatum.setIsPictureThreeFour(true);
                teacherAttendancePdfDatum.setIsTextThreeFour(false);
            } else {
                teacherAttendancePdfDatum.setIsPictureThreeFour(false);
                teacherAttendancePdfDatum.setIsTextThreeFour(true);
            }

            Object teacherSignFiveSix = teacherAttendancePdfDatum.getTeacherSignFiveSix();
            if (Objects.nonNull(teacherSignFiveSix) && teacherSignFiveSix.toString().contains("file")) {
                teacherAttendancePdfDatum.setTeacherSignFiveSix(Pictures.ofLocal(uploadService.getRealPath(teacherSignFiveSix.toString())).size(60, 30).center().create());
                teacherAttendancePdfDatum.setIsPictureFiveSix(true);
                teacherAttendancePdfDatum.setIsTextFiveSix(false);
            } else {
                teacherAttendancePdfDatum.setIsPictureFiveSix(false);
                teacherAttendancePdfDatum.setIsTextFiveSix(true);
            }

            Object teacherSignSevenEight = teacherAttendancePdfDatum.getTeacherSignSevenEight();
            if (Objects.nonNull(teacherSignSevenEight) && teacherSignSevenEight.toString().contains("file")) {
                teacherAttendancePdfDatum.setTeacherSignSevenEight(Pictures.ofLocal(uploadService.getRealPath(teacherSignSevenEight.toString())).size(60, 30).center().create());
                teacherAttendancePdfDatum.setIsPictureSevenEight(true);
                teacherAttendancePdfDatum.setIsTextSevenEight(false);
            } else {
                teacherAttendancePdfDatum.setIsPictureSevenEight(false);
                teacherAttendancePdfDatum.setIsTextSevenEight(true);
            }
        }
        return teacherAttendancePdfData;
    }

    private void  beasVerify(TeacherAttendanceDTO reqDTO){
        Integer count =0;
        count = baseMapper.selectCount(Wrappers.<TeacherAttendance>lambdaQuery()
                .eq(TeacherAttendance::getTrainingClassInfoId,reqDTO.getTrainingClassInfoId())
                .eq(TeacherAttendance::getSignDate,reqDTO.getSignDate())
                .eq(TeacherAttendance::getTimeOfDay,reqDTO.getTimeOfDay())
                .eq(TeacherAttendance::getSignLesson,reqDTO.getSignLesson())
                .eq(TeacherAttendance::getIsDeleted, DeleteCodeEnum.UN_DELETE.code)
                .ne(StringUtils.isNotBlank(reqDTO.getId()),TeacherAttendance::getId,reqDTO.getId())
        );
        if(count>0){
            throw new ServiceException(ApiError.ERROR_10010017);
        }
    }


}
